package ootexception;

import java.util.*;
import java.io.*;
import java.awt.*;
import util.*;

public class ExceptionHandler
{

static Hashtable exception_hashtable=new Hashtable();

public static void loadFromArray()
{
 int length=ExceptionArray.hashdat.length;

 for (int loop=0;loop<length;loop++)
 {
   String id =new String(ExceptionArray.hashdat[loop][0]);
   String msg=new String(ExceptionArray.hashdat[loop][1]);
   String url=new String(ExceptionArray.hashdat[loop][2]);
   ExceptionHashNode new_node=new ExceptionHashNode(msg,url);
   Object target=exception_hashtable.put(id,new_node);
 }

}

public void loadFromFile(String filename)
{

  StringTokenizer st1=null;
  try
  {
    BufferedReader br=new BufferedReader(new FileReader(filename));
    while (br.ready())
    {
       String s=br.readLine();
       StringBuffer sb=new StringBuffer(s);
       while (!s.endsWith("&&"))
       {
       s=br.readLine();
       sb.append(s);
       sb.append("\n");
       }
       s=sb.toString();
       sb.append("\n");


       st1=new StringTokenizer(s);
       String key=st1.nextToken("|");
       String msg=st1.nextToken("|");
       String url=st1.nextToken("|");
       st1=new StringTokenizer(url);
       url=st1.nextToken("&&");




       ExceptionHashNode new_node=new ExceptionHashNode(msg,url);
       String id=new String(key);
       Object target=exception_hashtable.put(id,new_node);

    }
  }
  catch(Exception e)
  {
    Debug.println(e.toString());
    Debug.println("System can not start");
  }




}


public static void showException(Frame f,Exception e)
  {

  String id=e.getClass().getName();

  Debug.println("Excepition type is a "+id+".");


  String oot=new String("OOT");


  if (oot.regionMatches(0,id,13,3))
  {

    OOT_Exception temp=(OOT_Exception)e;
    String short_id=id.substring(13);



    String keyname=temp.getKeyname();
    Debug.println("Keyname is a "+id+".");


    String newid=new String(short_id+keyname);

    Debug.println("Combined Key is a ["+newid+"].");

    Object next_id=exception_hashtable.get(newid);
    ExceptionHashNode ehn=(ExceptionHashNode)next_id;
    if (ehn==null) OOTErrorMsg.show("An exception occured. "+e.getMessage());
    else
    {
    ExceptionDialog ed=new ExceptionDialog(f,ehn);
    ed.show();
    }
  }
  else
  {
  ExceptionHashNode ehn=new ExceptionHashNode();
  ehn.msg=e.toString();
  ExceptionDialog ed=new ExceptionDialog(f,ehn);
  ed.show();
  }

  }

}
